clear all
clc

global H

load Data.mat
Data=data;
clear data

load Demo.mat
Demo=data;
D=size(Demo,2);
clear data

N=max(Data(:,1));
task=max(Data(:,2));
option=max(Data(:,3));
L=size(Data,2)-4+option-1;

S=10000;                              %number of simulation
yita=randn(N,option,S);
Scenario=[5 0 1; .5 0 0; 0 .5 0];

clogitbeta=fsolve('clogit',zeros(1,L),...
    [optimset('TolFun',1e-5, 'MaxFunEvals', 100000)],...
    Data);
clogiterror=sqrt(diag(Hessian(clogitbeta, Data)));
[clogitbeta' clogiterror]

%Share=share(mixedlogitbeta,Demo,Scenario)

%Set the individual Hessian of psuedo-likelihood to be used in MH
initial_guess=clogitbeta;
for n=1:N
    clogitbetan(n,:)=fsolve('clogit_star',initial_guess,...
        [optimset('TolFun',1e-5, 'MaxFunEvals', 100000)],Data,n);
    H(:,:,n)=Hessian_star(clogitbetan(n,:), Data,n);
end

%Draw inference from the 1-comp model using MCMC and MH algorithm
K=1;
Bayesbeta=bayesmixture(Data,Demo,clogitbeta,K);
save BayesBeta_normal_1_temp
clear Bayesbeta

%Draw inference from the 3-comp model using MCMC and MH algorithm
K=3;
Bayesbeta=bayesmixture(Data,Demo,clogitbeta,K);
save BayesBeta_normal_3_temp
clear Bayesbeta
